<?php
require_once("../system/funciones_bd.php");
require_once("../system/sesion.class.php");
//Conexion base de datos local
$link = mysql_connect("localhost", "root", "");
$sesion = new sesion();
$nombreCompleto = $sesion->get("nombre");
$nombreApellido = $sesion->get("apellidos");

$usuario = $nombreCompleto." ".$nombreApellido;
$usuarioId = $sesion->get("idusuario");
if ($usuario == false) {
    header("Location: ../index.php");
} else {
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("sirpof_db1", $link);
    mysql_set_charset('utf8', $link);
    ?>
    <!DOCTYPE html>
    <html lang="de"><!-- use class="debug" here if you develope a template and want to check-->
        <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
            <!-- some meta tags, important for SEO"--> 
            <meta name="description" content="put a short description in here" />
            <meta name="keywords" content="put your important keywords in here" />
            <meta name="revisit-after" content="7 days" />
            <meta name="robots" content="index,follow" />

            <title>ForensicTooth v1.0</title>


            <link rel="stylesheet" href="../css/inuit.css" />
            <link rel="stylesheet" href="../css/fluid-grid16-1100px.css" />
            <link rel="stylesheet" href="../css/eve-styles.css" />
            <link rel="shortcut icon" href="../icon.png" />
            <link rel="apple-touch-icon-precomposed" href="../img/icon.png" />
            <link rel="stylesheet" href="../css/jquery-ui.css" />
            <script src="../js/jquery-1.9.1.js"></script>
            <script src="../js/jquery-ui.js"></script>            
            <script src="../js/respond-min.js" type="text/javascript"></script>
        </script>
        <script>
            $(function() {
                $("#dialog").dialog({
                    autoOpen: false,
                    modal: true
                });
                $("#abrir")
                        .button()
                        .click(function() {
                            $("#dialog").dialog("open");
                        });
            });
        </script>
        <!--local fallback for JQuery-->

    </head>

    <!--===============================================================  Logo, social and menu =====================================================================================--> 
    <body>
        <div class="wrapper">	
            <a href="../index.php" id="logo"><img src="../img/logo.png" alt="something" />
                <h1 class="accessibility"></h1></a>
            <div class="social">
                <h2>
                <?php echo $sesion->get("usuario"); ?> 
                </h2>
            </div>


            <ul id="nav" class="main">
                <li><a href="#" class="active">Crear Paciente</a></li>
                <li><a href="principal.php">Regresar</a></li>
                <li><a href="../system/cerrarsesion.php">salir</a></li>
            </ul>

        </div><!--end of wrapper div--> 

        <div class="clear"></div>    
        <!--===============================================================  Intro =====================================================================================--> 
        <div class="wrapper">
            <div class="grids">
                <?php
                if (isset($_POST['btnGuardar'])) {
                    $activo = $_POST["activo"];
                    $pnombre = $_POST["pnombre"];
                    $snombre = $_POST["snombre"];
                    $papellido = $_POST["papellido"];
                    $sapellido = $_POST["sapellido"];
                    $tdocumento = $_POST["tdocumento"];
                    $ndocumento = $_POST["ndocumento"];
                    $genero = $_POST["genero"];
                    $fnacimiento = $_POST["fnacimiento"];
                    $lnacimiento = $_POST["lnacimiento"];
                    $dresidencia = $_POST["dresidencia"];
                    $bresidencia = $_POST["bresidencia"];
                    $tresidencia = $_POST["tresidencia"];
                    $movil = $_POST["movil"];
                    $idusuario = $_POST["idusuario"];
                    $mail = $_POST["mail"];
                    $fecharegistro = date("Y-m-d H:i:s");
                    if ($genero == '1') {
                        $foto = "imagen.png";
                    } else IF ($genero == '0') {
                        $foto = "imagen1.png";
                    }
                    $sqlproducto = "INSERT INTO paciente VALUES (NULL, '" . $idusuario . "', '" . $pnombre . "', '" . $snombre . "', '" . $papellido . "', '" . $sapellido . "', '" . $tdocumento . "', '" . $ndocumento . "', '" . $genero . "', '" . $fnacimiento . "', '" . $lnacimiento . "', '" . $dresidencia . "', '" . $bresidencia . "', '" . $tresidencia . "', '" . $movil . "', '" . $mail . "', '" . $fecharegistro . "', '" . $foto . "', '" . $activo . "');";
                    //echo $sqlproducto."<br />";
                    mysql_query($sqlproducto, $link);

                    $sqlres = "SELECT max(id_paciente) FROM paciente";
                    $sqlres1 = mysql_query($sqlres, $link);
                    $rowMax = mysql_fetch_array($sqlres1);
                    
                    //echo $sqlres."<br />";
                    $sqlodontograma = "insert into paciente_odontograma
                                        SELECT DISTINCT NULL, '$fecharegistro', $rowMax[0], 1 FROM paciente ";
                    mysql_query($sqlodontograma, $link);
                    //echo $sqlodontograma."<br />";
                    $sqlodontogramaDetalle = "insert into odontograma
                                        SELECT NULL , idlista,  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  '#FFFFFF',  (
                                            SELECT MAX( id_registro ) FROM paciente_odontograma
                                        ) AS id_registro, 1
                                        FROM lista WHERE id_grupo = 1";
                    //echo $sqlodontograma;
                    mysql_query($sqlodontogramaDetalle, $link);
                    header("Location: principal.php");
                }
                //Finalmente hacemos la insercion en otra tabla
                ?>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <table>
                        <thead>
                            <tr>
                                <th scope="col" colspan="2"><h2>CREAR PACIENTE</h2></th>
                        </tr>
                        </thead>
                        <tfoot>
                            <tr><td colspan="5">&nbsp;</td></tr>
                        </tfoot>
                        <tbody>
                            <tr>
                                <th scope="col" colspan="2"><input type="checkbox" name="activo" checked>Habilitado</input></th>
                            </tr>
                            <tr>
                                <th scope="col">*Primer Nombre</th>
                                <th scope="col"><input type="text" class="input" id="pnombre" name="pnombre" maxlength="20" required></th>
                            </tr>
                            <tr>
                                <th scope="col">Segundo Nombre</th>
                                <th scope="col"><input type="text" class="input" id="snombre" name="snombre" maxlength="20" /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Primer Apellido</th>
                                <th scope="col"><input type="text" class="input" id="papellido" name="papellido" maxlength="20" required/></th>
                            </tr>
                            <tr>
                                <th scope="col">Segundo Apellido</th>
                                <th scope="col"><input type="text" class="input" id="sapellido" name="sapellido" maxlength="20"/></th>
                            </tr>
                            <tr>
                                <th scope="col">*Genero</th>
                                <th scope="col">
                                    <select name="genero" class="input" required> 
                                        <option value="">Seleccione...</option>
                                        <option value="1">Masculino</option>
                                        <option value="0">Femenino</option>
                                    </select></th>
                            </tr>
                            <tr>
                                <th scope="col">*Tipo de Documento</th>
                                <th scope="col">
                                    <select name="tdocumento" class="input" required> 
                                        <option value="null">Seleccione...</option>
                                        <option value="1">Registro Civil</option>
                                        <option value="2">Tarjeta de Identidad</option>
                                        <option value="2">Cedula de Ciudadania</option>
                                        <option value="2">Cedula de Extranjeria</option>
                                        <option value="2">NUIP</option>
                                    </select>
                                </th>
                            </tr>
                            <tr>
                                <th scope="col">Número Documento</th>
                                <th scope="col"><input type="text" class="input" id="ndocumento" name="ndocumento" maxlength="10" required/></th>
                            </tr>
                            <tr>
                                <th scope="col">*Fecha Nacimiento</th>
                                <th scope="col"><input type="date" class="input" name="fnacimiento" required /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Lugar Nacimiento</th>
                                <th scope="col"><input type="text" class="input" id="lnacimiento" name="lnacimiento" maxlength="120" required /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Direccion Residencia</th>
                                <th scope="col"><input type="text" class="input" id="dresidencia" name="dresidencia" maxlength="45" required /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Barrio Residencia</th>
                                <th scope="col"><input type="text" class="input" id="bresidencia" name="bresidencia" maxlength="60" required /></th>
                            </tr>
                            <tr>
                                <th scope="col">*Telefono Residencia</th>
                                <th scope="col"><input type="text" class="input" id="tresidencia" name="tresidencia" maxlength="10" required/></th>
                            </tr>
                            <tr>
                                <th scope="col">E-Mail</th>
                                <th scope="col"><input type="email" class="input" id="tresidencia" name="mail" maxlength="110" /></th>
                            </tr>
                            <tr>
                                <th scope="col">Movil</th>
                                <th scope="col"><input type="text" class="input" id="movil" name="movil" maxlength="12" /></th>
                            </tr>
                            <?php
                            
                            ?>
                                <input type="hidden" name="idusuario" value="<?php echo $usuarioId; ?>" />
                            <tr>
                                <th scope="col" colspan="2">
                                    <input type="submit" value="Guardar" name="btnGuardar" class="button" />
                                    <input type="reset" value="Limpiar" name="btnLimpiar" class="button" />
                                </th>
                            </tr>

                        </tbody>
                    </table>

                </form>


            </div><!--end of div grids-->

        </div><!--end of div wrapper-->    

        <!--===============================================================  Footer =====================================================================================-->  
        <div class="wrapper">
            <div id="footer">


                <div class="grids">
                    <center>
                        <p>ForensicTooth v 1.0</p>
                    </center>

                </div><!--end of grids-->
            </div><!--end of footer-->
        </div><!--end of wrapper-->



    </body>
    </html>

    <?php
}
?>